#include "InputLine.h"
#include <list>

using namespace std;

const string InputLine::delimiters = "()[]{};<>=+-*& \t";
const string InputLine::doubleDelimiters = "+-=<>";
//string InputLine::specialDelimiter = "->";

InputLine::InputLine(const string& line) : line(line) 
{
	this->lst = new list<const string*>;
	int i = 0, j=0, step=1;
	int pos = 0;
	while(pos<line.size())
	{
		step = 1;
		string *sub = new string();
		string *sub2 = new string();
		i = line.find_first_of(delimiters,pos);
		if (i==string::npos)
		{
			*sub = line.substr(pos, line.size()-pos);
			(this->lst)->push_back(sub);
			break;
		}
		*sub = line.substr(pos, i-pos); // for Copying until the delimiter
		*sub2 = line.substr(i, 1); // for Copying the delimiter

		j = line.find_first_of(doubleDelimiters,pos); // Case of ++, -- etc.
		if (i==j && i+1 != line.size()) // If we have a POTENTIALLY double delimiter
		{
			if ((line.at(i) == line.at(i+1)) || ((line.at(i) == '-') && (line.at(i+1) == '>')) )
			{
				*sub2 = line.substr(i, 2);
				step = 2;
			}
		}
		
		if (sub->size()>0 && sub[0]!=" " && sub[0]!="	")	(this->lst)->push_back(sub);
		if (sub2->size()>0 && sub2[0]!=" " && sub2[0]!="	") (this->lst)->push_back(sub2);
		pos = i+step;
	}
}

InputLine::~InputLine()
{
	for (list<const string*>::iterator iter = (this->lst)->begin(); iter!=this->lst->end(); iter++)
	{
		delete *iter;
	}
	delete this->lst;
}

const list<const string*> InputLine::getList() const
{ 
	return *lst;
}



